package zuoye;

/**
 * 3：新增学生，通过控制台输入信息，保存到student表中。
 */
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class insertStudent {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		DBUtil dbUtil = null;
		Date date = null;
		Scanner input = new Scanner(System.in);
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("请输入要输入的学生姓名：");
		String sname = input.next();
		System.out.println("请输入要输入的性别：");
		String sex = input.next();
		System.out.println("请输入要输入的手机号：");
		String telephone = input.next();
		System.out.println("请输入要输入的QQ号：");
		String qq = input.next();
		System.out.println("请输入要输入的微信号：");
		String wechat = input.next();
		System.out.println("请输入要输入的年级编号：");
		int gradeid = input.nextInt();
		System.out.println("请输入要输入的入学日期：");
		String enterdate = input.next();
		System.out.println("请输入要输入的籍贯：");
		String fromcity = input.next();
		System.out.println("请输入要输入的出生年月：");
		String birthday = input.next();
		System.out.println("请输入要输入的地址：");
		String address = input.next();
		System.out.println("请输入要输入的密码：");
		String pass = input.next();
		// String url = "jdbc:mysql://localhost:3306/myschool";
		String sql = "insert into student(sname,sex,telephone,qq,wechat,gradeid"
				+ ",enterdate,fromcity,birthday,address,pass) "
				+ "values(?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt = null;
		Connection conn = null;
		try {
			conn = dbUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			System.out.println(sql);
			pstmt.setString(1, sname);
			pstmt.setString(2, sex);
			pstmt.setString(3, telephone);
			pstmt.setString(4, qq);
			pstmt.setString(5, wechat);
			pstmt.setInt(6, gradeid);
			pstmt.setDate(7, date.valueOf(enterdate));
			pstmt.setString(8, fromcity);
			pstmt.setDate(9, date.valueOf(birthday));
			pstmt.setString(10, address);
			pstmt.setString(11, pass);
			pstmt.executeUpdate();
			System.out.println("连接成功");
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			System.out.println("连接失败");
		} finally {
			dbUtil.close(conn);
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}
	}

}
